package com.chinawlx.wlxteacher.widget.nine_grid;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.chinawlx.wlxteacher.R;
import com.chinawlx.wlxteacher.ui.activity.WLXPhotoViewActivity;

import java.util.ArrayList;

/**
 * 作者:黄帅
 * 时间:16/9/23
 * 描述:多图片自适应显示控件,最多九张
 */
public class WLXGridLayout extends GridLayout {

    protected static final int MAX_W_H_RATIO = 3;
    private Context mContext;

    public WLXGridLayout(Context context) {
        super(context);
        this.mContext = context;
    }

    public WLXGridLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
    }

    /**
     * 设置最多展示三张图片,不设置默认就是九张
     */
    public void setMaxImageNumTo3() {
        setMaxCount(3);
    }


    /**
     * 一张图片的时候会调用的,这里可以控制单个图片的宽高
     *
     * @param imageView
     * @param url
     * @param parentWidth 父控件宽度
     * @return
     */
    @Override
    protected boolean displayOneImage(final RatioImageView imageView, String url, final int
            parentWidth) {
        Glide.with(mContext).load(url).placeholder(R.drawable
                .place_holder).centerCrop().listener(new RequestListener<String, GlideDrawable>() {
            @Override
            public boolean onException(Exception e, String model, Target<GlideDrawable> target,
                                       boolean isFirstResource) {
                return false;
            }

            @Override
            public boolean onResourceReady(GlideDrawable bitmap, String model,
                                           Target<GlideDrawable> target, boolean isFromMemoryCache,
                                           boolean isFirstResource) {
                int w = bitmap.getMinimumWidth();
                int h = bitmap.getMinimumHeight();

                int newW;
                int newH;
                if (h > w * MAX_W_H_RATIO) {//h:w = 5:3
                    newW = parentWidth / 2;
                    newH = newW * 5 / 3;
                } else if (h < w) {//h:w = 2:3
                    newW = parentWidth * 2 / 3;
                    newH = newW * 2 / 3;
                } else {//newH:h = newW :w
                    newW = parentWidth / 2;
                    newH = h * newW / w;
                }
                setOneImageLayoutParams(imageView, parentWidth, newH);
                return false;
            }
        }).into(imageView);
        return false;
    }

    @Override
    protected void displayImage(RatioImageView imageView, String url) {
        Glide.with(mContext).load(url).placeholder(R.drawable
                .place_holder).centerCrop().into(imageView);
    }

    @Override
    protected void onClickImage(int position, String url, ArrayList<String> urlList) {
        Intent intent = new Intent(
                mContext, WLXPhotoViewActivity.class);
        intent.putExtra(WLXPhotoViewActivity.URLS, urlList);
        intent.putExtra(WLXPhotoViewActivity.POSITION,position);
        mContext.startActivity(intent);
    }
}
